package com.redog.dao;

import com.redog.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class BookDaoImp implements BookDao {

    //注入jdbcTemplate
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void add(User user) {
        //创建sql语句
        String sql = "insert into t_user values(?,?,?)";
        Object[] args = {user.getUserId(),user.getUsername(),user.getUstatus()};
        int update = jdbcTemplate.update(sql, args);
        //打印结果（影响行数）
        System.out.println(update);
    }

    @Override
    public void updataUser(User user) {
        String sql = "update t_user set username=?,ustatus=? where user_id=?";
        Object[] args = {user.getUsername(), user.getUstatus(),user.getUserId()};
        int update = jdbcTemplate.update(sql, args);
        System.out.println(update);
    }

    @Override
    public void deleteUser(String id) {
        String sql = "delete from t_user where user_id=?";
        int update = jdbcTemplate.update(sql, id);
        System.out.println(update);
    }

    @Override
    public User findUserInfo(String id) {
        String sql = "select * from t_user where user_id=?";
        //调用方法
        /**
         * new BeanPropertyRowMapper<User>(User.class)做了什么？
         * 把从数据库调的数据用User类中的set方法存到user实例中
         *
         *
         * 重点User类的设计一定要规范，否则框架不知道如何把数据库中的数据映射到java类上
         */

        User user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), id);
        return user;
    }
}
